﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Reflection;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Text;
using System.IO;

namespace CSharpReflector
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //HelloWorld();
            //Response.Write(Request.Path.Substring(Request.Path.LastIndexOf("/") + 1));
            Test1();
        }

        public void Test1()
        {
            Assembly objAssembly;
            if (!File.Exists("d:\\a.dll"))
            {
                CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
                CompilerParameters objCompilerParameters = new CompilerParameters();
                objCompilerParameters.ReferencedAssemblies.Add("System.dll");//引用dll 
                objCompilerParameters.GenerateExecutable = false;
                //objCompilerParameters.GenerateInMemory = true;
                //则动态编译的dll会保存到指定位置
                objCompilerParameters.GenerateInMemory = false;
                objCompilerParameters.OutputAssembly = "d:\\a.dll";
                //编译 
                CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, new string[] { GenerateCode() });

                if (cr.Errors.HasErrors)
                {
                    Response.Write("编译错误：");
                    foreach (CompilerError err in cr.Errors)
                    {
                        Response.Write(err.ErrorText);
                    }
                }
                objAssembly = cr.CompiledAssembly;
            }
            else
            {
                objAssembly = Assembly.LoadFrom("d:\\a.dll");
            }
            object objHelloWorld = objAssembly.CreateInstance("DynamicCodeGenerate.HelloWorld");
            MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");
            // 调用执行 
            Response.Write(objMI.Invoke(objHelloWorld, null));
        }

        public static string GenerateCode()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("using System;");
            sb.Append(Environment.NewLine);
            sb.Append("namespace DynamicCodeGenerate");
            sb.Append(Environment.NewLine);
            sb.Append("{");
            sb.Append(Environment.NewLine);
            sb.Append(" public class HelloWorld");
            sb.Append(Environment.NewLine);
            sb.Append(" {");
            sb.Append(Environment.NewLine);
            sb.Append(" public string OutPut()");
            sb.Append(Environment.NewLine);
            sb.Append(" {");
            sb.Append(Environment.NewLine);
            sb.Append(" return \"Hello world!\";");
            sb.Append(Environment.NewLine);
            sb.Append(" }");
            sb.Append(Environment.NewLine);
            sb.Append(" }");
            sb.Append(Environment.NewLine);
            sb.Append("}");

            string code = sb.ToString();
            Console.WriteLine(code);
            Console.WriteLine();

            return code;
        }

        public void HelloWorld()
        {
            Response.Write("Hello World!!!!!");
        }
    }
}
